//
//  LocalSetting.swift
//  VPai
//
//  Created by devel on 16/9/26.
//  Copyright © 2016年 viatech. All rights reserved.
//

import Foundation
import player360

class LocalSetting: NSObject {
    
    private static let KEY_PLAYER_GSENSOR_ON = "key_player_gsensor_on"
    private static let KEY_PLAYER_TOUCH_ON = "key_player_touch_on"
    private static let KEY_DISPLAY_MODE = "key_display_mode"
    private static let KEY_PLAYER_VR_ON = "key_player_vr_on"
    
    private var listeners = ListenerList()
    
    //单例模式
    class var shareInstance : LocalSetting{
        struct Static {
            static var instance: LocalSetting?
            static var token: dispatch_once_t = 0
        }
        
        dispatch_once(&Static.token){
            Static.instance = LocalSetting()
        }
        
        return Static.instance!
    }
    
    //是否开启陀螺仪
    var playerGsensorOn : Bool = true{
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setBool(playerGsensorOn, forKey: LocalSetting.KEY_PLAYER_GSENSOR_ON)
            objc_sync_enter(self)
            dispatch_async(dispatch_get_main_queue(), {
                self.listeners.foreach(){(l: LocalSettingListener, index: Int) in
                    l.onPlayerGsensorOnChange()
                }
            })
            objc_sync_exit(self)
        }
    }
    
    var playerTouchOn : Bool = false{
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setBool(playerTouchOn, forKey: LocalSetting.KEY_PLAYER_TOUCH_ON)
            objc_sync_enter(self)
            dispatch_async(dispatch_get_main_queue(), {
                self.listeners.foreach(){(l: LocalSettingListener, index: Int) in
                    l.onPlayerTouchOnChange()
                }
            })
            objc_sync_exit(self)
        }
    }
    
    //public let MODE_PANORAMA = 0
    //public let MODE_PLANET = 1
    //public let MODE_FISHEYE = 2
    var displayMode : Int = MODE_PANORAMA {
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setInteger(displayMode, forKey: LocalSetting.KEY_DISPLAY_MODE)
            objc_sync_enter(self)
            dispatch_async(dispatch_get_main_queue(), {
                self.listeners.foreach(){(l: LocalSettingListener, index: Int) in
                    l.onDisplayModeChange()
                }
            })
            objc_sync_exit(self)
        }
    }
    
    var playerVROn : Bool = false {
        didSet{
            let defs = NSUserDefaults.standardUserDefaults()
            defs.setBool(playerVROn, forKey: LocalSetting.KEY_PLAYER_VR_ON)
            objc_sync_enter(self)
            dispatch_async(dispatch_get_main_queue(), {
                self.listeners.foreach(){(l: LocalSettingListener, index: Int) in
                    l.onPlayerVROnChange()
                }
            })
            objc_sync_exit(self)
        }
    }
    
    func resistLocalSettingListener(listener: LocalSettingListener){
        objc_sync_enter(self)
        listeners.registerListener(listener)
        objc_sync_exit(self)
    }
    
    func unresistLocalSettingListener(listener: LocalSettingListener){
        objc_sync_enter(self)
        listeners.unregisterListener(listener)
        objc_sync_exit(self)
    }
    
    private override init(){
        super.init()
        
        let defs = NSUserDefaults.standardUserDefaults();

        playerGsensorOn = defs.boolForKey(LocalSetting.KEY_PLAYER_GSENSOR_ON)
        playerTouchOn = defs.boolForKey(LocalSetting.KEY_PLAYER_TOUCH_ON)
        displayMode = defs.integerForKey(LocalSetting.KEY_DISPLAY_MODE)
        playerVROn = defs.boolForKey(LocalSetting.KEY_PLAYER_VR_ON)
        
        if !playerGsensorOn &&  !playerTouchOn {
            playerGsensorOn = true
            playerTouchOn = true
        }
        
        if displayMode < 0 || displayMode >= SUM_MODE{
            displayMode = MODE_PANORAMA
        }
    }
}

protocol LocalSettingListener: NSObjectProtocol {
    func onPlayerGsensorOnChange()
    func onPlayerTouchOnChange()
    func onPlayerVROnChange()
    func onDisplayModeChange()
}

extension LocalSettingListener {
    func onPlayerGsensorOnChange(){}
    func onPlayerTouchOnChange(){}
    func onPlayerVROnChange(){}
    func onDisplayModeChange(){}
}